package com.cryptoneedle.garden.service.garden.doris;


import cn.hutool.v7.core.bean.BeanUtil;
import com.bubbles.engine.data.core.repository.BaseRepository;
import com.bubbles.engine.data.core.service.BaseService;
import com.cryptoneedle.garden.entity.garden.doris.DorisColumn;
import com.cryptoneedle.garden.key.ColumnKey;
import com.cryptoneedle.garden.repository.doris.DorisInformationColumnRepository;
import com.cryptoneedle.garden.repository.garden.doris.DorisColumnRepository;
import com.cryptoneedle.garden.service.crud.garden.doris.*;
import com.google.common.collect.Maps;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>description: Doris Column </p>
 *
 * @author CryptoNeedle
 * @date 2025-09-25
 */
@Service
@Slf4j
public class DorisColumnService extends BaseService<DorisColumn, ColumnKey> {

    @Resource
    private DorisColumnRepository dorisColumnRepository;

    @Override
    public BaseRepository<DorisColumn, ColumnKey> getRepository() {
        return this.dorisColumnRepository;
    }

    @Autowired
    private AddDorisService add;
    @Autowired
    private DeleteDorisService delete;
    @Autowired
    private FoundDorisService select;
    @Autowired
    private PatchDorisService patch;
    @Autowired
    private SaveDorisService save;

    @Resource
    private DorisInformationColumnRepository dorisInformationColumnRepository;

    public List<DorisColumn> syncDorisColumn(String catalogName, String databaseName) {
        // 查询所有
        List<DorisColumn> originList = select.dorisColumns(catalogName, databaseName);

        // 待同步数据
        List<DorisColumn> dealList = dorisInformationColumnRepository.dorisInformationColumn(catalogName, databaseName)
                .stream()
                .map(deal -> BeanUtil.copyProperties(deal, DorisColumn.class))
                .toList();

        // 比较插入
        syncDorisColumnCompare(originList, dealList);

        return select.dorisColumns(catalogName, databaseName);
    }

    public List<DorisColumn> syncDorisColumn(String catalogName, String databaseName, String tableName) {
        // 查询所有
        List<DorisColumn> originList = select.dorisColumns(catalogName, databaseName, tableName);

        // 待同步数据
        List<DorisColumn> dealList = dorisInformationColumnRepository.dorisInformationColumn(catalogName, databaseName, tableName)
                .stream()
                .map(deal -> BeanUtil.copyProperties(deal, DorisColumn.class))
                .toList();

        syncDorisColumnCompare(originList, dealList);

        return select.dorisColumns(catalogName, databaseName, tableName);
    }

    public void syncDorisColumnCompare(List<DorisColumn> originList, List<DorisColumn> dealList) {
        Map<String, DorisColumn> originMap = Maps.uniqueIndex(originList, DorisColumn::columnKeyString);
        Map<String, DorisColumn> dealMap = Maps.uniqueIndex(dealList, DorisColumn::columnKeyString);

        // 新增数据
        List<DorisColumn> addList = dealList.stream().filter(deal -> !originMap.containsKey(deal.columnKeyString())).toList();

        // 移除数据
        List<DorisColumn> removeList = originList.stream().filter(item -> !dealMap.containsKey(item.columnKeyString())).toList();

        // 保存数据
        List<DorisColumn> saveList = originList
                .stream()
                .filter(origin -> dealMap.containsKey(origin.columnKeyString()))
                .peek(origin -> {
                    DorisColumn deal = dealMap.get(origin.columnKeyString());
                    origin.setCatalogName(deal.getCatalogName())
                            .setDatabaseName(deal.getDatabaseName())
                            .setTableName(deal.getTableName())
                            .setColumnName(deal.getColumnName())
                            .setOrdinalPosition(deal.getOrdinalPosition())
                            .setColumnDefault(deal.getColumnDefault())
                            .setDataType(deal.getDataType())
                            .setCharacterMaximumLength(deal.getCharacterMaximumLength())
                            .setCharacterOctetLength(deal.getCharacterOctetLength())
                            .setNumericPrecision(deal.getNumericPrecision())
                            .setNumericScale(deal.getNumericScale())
                            .setDatetimePrecision(deal.getDatetimePrecision())
                            .setColumnType(deal.getColumnType())
                            .setColumnKey(deal.getColumnKey())
                            .setExtra(deal.getExtra())
                            .setColumnComment(deal.getColumnComment())
                            .setColumnSize(deal.getColumnSize())
                            .setDecimalDigits(deal.getDecimalDigits());
                }).toList();

        add.dorisColumns(addList);
        save.dorisColumns(saveList);
        delete.dorisColumns(removeList);
    }
}